package com.qf.controller;

import com.qf.dto.UserDTO;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * 用于接收参数
 * @author zjw
 * @description
 * @date 2021/10/8$ 11:13$
 */
@Controller
@RequestMapping("/parameter")
public class ParameterController {

    /**
     * http://localhost:8080/parameter/simple?name=zhangsan&age=23&birthday=2011-11-11
     * @return
     */
    @RequestMapping("/simple")
    public ModelAndView simple(String name, Integer age, @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday){
        System.out.println("xxxx");
        return null;
    }


    /**
     * http://localhost:8080/parameter/annotation?name=zhangsan&age=23&birthday=2011-11-11
     * @return
     */
    @RequestMapping("/annotation")
    public ModelAndView annotation(@RequestParam(value = "name",required = true,defaultValue = "张三") String username,
                                   @RequestParam Integer age,
                                   @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday){
        System.out.println("xxxx");
        return null;
    }

    /**
     * http://localhost:8080/parameter/pojo?name=zhangsan&age=23&birthday=2011-11-11
     * @return
     */
    @RequestMapping("/pojo")
    public ModelAndView pojo(@Valid UserDTO userDTO, BindingResult bindingResult){
        // 判断是否有参数未满足要求
        if(bindingResult.hasErrors()){
            // 有参数不合法
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
        }
        System.out.println("xxxx");
        return null;
    }

    /**
     * http://localhost:8080/parameter/origin?name=zhangsan&age=23&birthday=2011-11-11
     * @return
     */
    @RequestMapping("/origin")
    public ModelAndView origin(HttpServletRequest req){
        Map<String, String[]> parameterMap = req.getParameterMap();
        System.out.println("xxxx");
        return null;
    }
}
